<template>
  <div class="app-container">
    <table-search
      :data="list"
      :loading="loadingTableVisible"
      :total="total"
      :page="listQuery.page"
      :limit="listQuery.limit"
      @onSearch="getArticles"
    >
      <template slot="search">
        <el-input
          class="filter-item"
          v-model="listQuery.search"
          clearable
          placeholder="标题/标签"
          style="width:200px"
          size="mini"
        />
      </template>

      <template slot="button">
        <router-link :to="'/article/create'">
          <el-button class="filter-item" type="primary" icon="el-icon-edit" size="mini">添加</el-button>
        </router-link>
      </template>

      <template slot="column">
        <el-table-column label="编号" width="80" prop="id" align="center"></el-table-column>
        <el-table-column label="标题">
          <template slot-scope="scope">
            <a class="table-title" href="javascript:void(0)">{{scope.row.title}}</a>
          </template>
        </el-table-column>
        <el-table-column label="标签">
          <template slot-scope="scope">
            <el-tag size="mini" closable v-for="item in scope.row.tags">{{item}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="浏览量" width="80" prop="flow"></el-table-column>
        <el-table-column label="状态" width="80">
          <template slot-scope="scope">{{ pasetStaus(scope.row.status)}}</template>
        </el-table-column>
        <el-table-column label="创建日期" width="150px" align="center">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span>{{ scope.row.creationTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{row}">
            <router-link :to="'/article/edit/'+row.id">
              <el-button type="primary" class="list-mini-btn">编辑</el-button>
            </router-link>
            <el-button type="danger" class="list-mini-btn" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </table-search>
  </div>
</template>

<script>
import { getArticleList, deleteArticleInfo } from "@/api/blog/article";
import TableSearch from "@/components/TableSearch";
export default {
  components: { TableSearch },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        search: undefined
      },
      loadingTableVisible: false
    };
  },
  created() {
    this.getArticles();
  },
  methods: {
    getArticles() {
      this.loadingTableVisible = true;
      getArticleList(this.listQuery)
        .then(response => {
          this.loadingTableVisible = false;
          this.list = response.data.items;
          this.total = response.data.total;
        })
        .catch(err => {
          this.loadingTableVisible = false;
          console.error(err);
        });
    },
    handleDelete(row) {
      this.$confirm("确定删除文章？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const loading = this.$loading({ text: "正在删除数据" });
          deleteArticleInfo(row.id)
            .then(response => {
              loading.close();
              this.$message({
                message: response.message,
                type: response.success ? "success" : "error"
              });
            })
            .catch(err => {
              loading.close();
              console.error(err);
            });
        })
        .catch(err => {
          console.error(err);
        });
    },
    pasetStaus(status) {
      return this.$enums["saaS.Entity.Enums.ArticleStatus"][status];
    }
  }
};
</script>

<style scoped>
.cell .el-tag {
  margin-right: 10px;
}
.table-title {
  text-decoration: underline;
}
.table-title:hover {
  color: #1890ff;
}
</style>